//
//  MenuController.m
//  citzenWorkStation
//
//  Created by yang qing on 2018/2/10.
//  Copyright © 2018年 yang qing. All rights reserved.
//

#import "MenuController.h"
#import "UIViewController+LGSideMenuController.h"
#import "SlideViewController.h"
#import "UIColor+Hex.h"
#import "LoginViewController.h"
#import "DataSaveUtils.h"
#import "UIView+Layout.h"
#import "PersonInfoViewController.h"
#import "API.h"
#import "SettingViewController.h"
#import "QuitDialog.h"
#import "IdVerfityViewController.h"
#import <SDWebImage/UIImageView+WebCache.h>
@interface MenuController ()<QuitDialogDelegate>
@property (weak, nonatomic) IBOutlet UIView *head_bg_view;
@property (weak, nonatomic) IBOutlet UIView *addressView;
@property (weak, nonatomic) IBOutlet UIView *adviceView;
@property (weak, nonatomic) IBOutlet UIView *settingView;
@property (weak, nonatomic) IBOutlet UIView *quitView;
@property (weak, nonatomic) IBOutlet UILabel *labelName;
@property (weak, nonatomic) IBOutlet UIImageView *ivSattus;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *StatusHeightConst;
@property (weak, nonatomic) IBOutlet UIImageView *ivHeadImage;
@property (strong,nonatomic) UserInfoBean *userInfo;
@end

@implementation MenuController
bool isLogin = NO;
- (void)viewDidLoad {
    [super viewDidLoad];
    _head_bg_view.backgroundColor = [UIColor colorWithHexString:ThemeColor];
    //设置圆角
    //设置图片圆角
    _ivHeadImage.layer.cornerRadius = _ivHeadImage.frame.size.width/2.0;
    _ivHeadImage.layer.masksToBounds = YES;
    [self initListener];
}

- (void)initListener{
    [_quitView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(quitClick)]];
    _labelName.userInteractionEnabled = YES;
    [_labelName addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(jumpToLoginView)]];
    _ivHeadImage.userInteractionEnabled = YES;
    [_ivHeadImage addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(headClick)]];
    [_settingView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(settingClick)]];
    _ivSattus.userInteractionEnabled = YES;
    [_ivSattus addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ivStatusClick)]];
}

- (void)jumpToLoginView{
    if([_labelName.text isEqualToString:@"请登录"]){
        //跳转到登录界面
        [self closeMenu];
        LoginViewController *loginVc = [[LoginViewController alloc]init];
        UINavigationController *navLogin = [[UINavigationController alloc]initWithRootViewController:loginVc];
        [self presentViewController:navLogin animated:YES completion:nil];
    }
}

- (void)headClick{
    if(isLogin){
        PersonInfoViewController *personVc = [[PersonInfoViewController alloc]init];
           UINavigationController *rootVc = [[UINavigationController alloc]initWithRootViewController:personVc];
         [self closeMenu];
          [self presentViewController:rootVc animated:YES completion:nil];
    }else{
        [self jumpToLoginView];
    }
}
- (void)ivStatusClick{
    if(_userInfo.auth==2){//未实名认证
        IdVerfityViewController * idVc = [[IdVerfityViewController alloc] init];
        UINavigationController *root = [[UINavigationController alloc] initWithRootViewController:idVc];
        [self closeMenu];
        [self presentViewController:root animated:YES completion:nil];
    }
  
}

- (void)settingClick{
    SettingViewController *settingVc = [[SettingViewController alloc]init];
    UINavigationController *rootVc = [[UINavigationController alloc]initWithRootViewController:settingVc];
    [self closeMenu];
       [self presentViewController:rootVc animated:YES completion:nil];
}

- (void)quitClick{
    QuitDialog *quitDialog = [[QuitDialog alloc] init];
    quitDialog.delegate = self;
    [quitDialog show];
}

//退出对话框点击代理
- (void)itemClick:(QuitDialog *)quitDialog type:(NSInteger)clickType{
    [quitDialog hide];
    if(clickType == ClickTypeQuit){
 
        [DataSaveUtils clearUserInfo];
        [self showToast:@"已退出"];
        [self changeStatus:nil];
        [self closeMenu];
    }else{
        [self closeMenu];
        LoginViewController *loginVc = [[LoginViewController alloc]init];
        UINavigationController *navLogin = [[UINavigationController alloc]initWithRootViewController:loginVc];
        [self presentViewController:navLogin animated:YES completion:nil];
    }
}

- (void)closeMenu{
    SlideViewController *slideVc =(SlideViewController *)self.sideMenuController;
    [slideVc hideLeftViewAnimated];
}



- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    //获取登录信息
    UserInfoBean *userInfo =[DataSaveUtils getUserInfo];
    _userInfo = userInfo;
    [self changeStatus:userInfo];
}

- (void)changeStatus:(UserInfoBean *)userInfo{
    if(userInfo){
        _labelName.text=userInfo.phoneNumber;
        _quitView.hidden = NO;
        self.ivSattus.hidden = NO;
        self.StatusHeightConst.constant = 33;
        isLogin  = YES;
        //头像
        NSString *baseImgeUrl = [FILE_BASE_URL stringByAppendingString:@"/get"];
        NSString *headImageUrl = [baseImgeUrl stringByAppendingString:userInfo.headImgUrl];
        [_ivHeadImage sd_setImageWithURL:[NSURL URLWithString:headImageUrl] placeholderImage:[UIImage imageNamed:@"icon_default_avatar"]];
        //用户信息状态
        switch (userInfo.auth) {
            case 1:
                [_ivSattus setImage:[UIImage imageNamed:@"icon_verified"]];
                break;
            case 2:
                [_ivSattus setImage:[UIImage imageNamed:@"icon_notvierify"]];
                break;
            case 3:
                [_ivSattus setImage:[UIImage imageNamed:@"icon_vierifing"]];
                break;
            default:
                [_ivSattus setImage:[UIImage imageNamed:@"icon_notvierify"]];
                break;
        }
    }else{
        _labelName.text=@"请登录";
        _quitView.hidden = YES;
        self.ivSattus.hidden = YES;
        self.StatusHeightConst.constant = 0;
        isLogin = NO;
        [_ivHeadImage sd_setImageWithURL:nil placeholderImage:[UIImage imageNamed:@"icon_default_avatar"]];
    }
}

@end
